package org.goldenport.g3.adapters

import org.goldenport.util.QSymbol
import org.goldenport.g3._
import org.goldenport.g3.messages._
import org.goldenport.g3.messages.event._
import org.goldenport.g3.events._
import org.goldenport.g3.events.event._

/**
 * @since   Nov. 21, 2010
 * @version Nov. 22, 2010
 * @author  ASAMI, Tomoharu
 */
class WebSocketAdapter(val driver: G3WebSocketDriver) extends G3Adapter {
  override def open_Adapter() {
    driver.open(context)
  }

  override def close_Adapter() {
    driver.close()
  }

  override protected def do_Action(content: AnyRef, parcel: G3Parcel, ctx: G3AgentContext): AnyRef = {
    content match {
      case cmd: G3Command => driver.execute(cmd, ctx)
    }
  }
}

abstract class G3WebSocketDriver extends G3Driver {
  protected def new_Agent(cmd: G3Command, ctx: G3AgentContext): G3WebSocketDriverAgent
}

abstract class G3WebSocketDriverAgent(uri: String, context: G3AgentContext) extends G3DriverAgent(uri, context) with UseRecord {
  def get(get: Get): G3Event = {
    error("")
  }

  def put(put: Put): G3Event = {
    error("")
  }

  def delete(delete: Delete): G3Event = {
    error("")
  }

  def mkcol(mkcol: Mkcol): G3Event = {
    error("")
  }
}

object NullWebSocketDriver extends G3WebSocketDriver {
  protected def new_Agent(cmd: G3Command, ctx: G3AgentContext): G3WebSocketDriverAgent = {
    new NullWebSocketDriverAgent(cmd.uri, ctx)
  }
}

class NullWebSocketDriverAgent(uri: String, ctx: G3AgentContext) extends G3WebSocketDriverAgent(uri, ctx) {
  def post(post: Post): G3Event = {
    Posted(post.uri)
  }
}
